package com.rhb.af.web.util;

import org.apache.commons.beanutils.Converter;
import java.text.SimpleDateFormat;

public class DateConverter implements Converter {
	static SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
	static SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	public DateConverter(){}

	public Object convert(Class type,Object value) {
		if(value==null) return null;
		if(!"String".equals(value.getClass().getSimpleName())) return null;
		
		java.util.Date date = null;
		
  		try{
  			String[] str = value.toString().split(":");
  			if(str.length==1){
  				date = dateFormat1.parse((String)value);
  			}else if(str.length==2){
  				date = dateFormat2.parse((String)value);
  			}else if(str.length==3){
  				date = dateFormat3.parse((String)value);
  			}
  			
  			//System.out.println("date = " + date);
  			
  			return date;      
  		}catch (Exception ex){
  			//ex.printStackTrace();
  		}
		return null;  			
  	}	
}